package com.ruoyi.system.service.impl;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.CoordinateUtil;
import com.ruoyi.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysOutletsMapper;
import com.ruoyi.system.domain.SysOutlets;
import com.ruoyi.system.service.ISysOutletsService;

/**
 * 服务网点Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-05-26
 */
@Service
public class SysOutletsServiceImpl implements ISysOutletsService 
{
    @Autowired
    private SysOutletsMapper sysOutletsMapper;

    /**
     * 查询服务网点
     * 
     * @param id 服务网点ID
     * @return 服务网点
     */
    @Override
    public SysOutlets selectSysOutletsById(Long id)
    {
        return sysOutletsMapper.selectSysOutletsById(id);
    }

    /**
     * 查询服务网点列表
     * 
     * @param sysOutlets 服务网点
     * @return 服务网点
     */
    @Override
    public List<SysOutlets> selectSysOutletsList(SysOutlets sysOutlets)
    {
        return sysOutletsMapper.selectSysOutletsList(sysOutlets);
    }

    /**
     * 新增服务网点
     * 
     * @param sysOutlets 服务网点
     * @return 结果
     */
    @Override
    public int insertSysOutlets(SysOutlets sysOutlets)
    {
        sysOutlets.setCreateTime(DateUtils.getNowDate());
        return sysOutletsMapper.insertSysOutlets(getCoordinate(sysOutlets));
    }

    /**
     * 修改服务网点
     * 
     * @param sysOutlets 服务网点
     * @return 结果
     */
    @Override
    public int updateSysOutlets(SysOutlets sysOutlets)
    {
        sysOutlets.setUpdateTime(DateUtils.getNowDate());
        return sysOutletsMapper.updateSysOutlets(getCoordinate(sysOutlets));
    }

    /**
     * 批量删除服务网点
     * 
     * @param ids 需要删除的服务网点ID
     * @return 结果
     */
    @Override
    public int deleteSysOutletsByIds(Long[] ids)
    {
        return sysOutletsMapper.deleteSysOutletsByIds(ids);
    }

    /**
     * 删除服务网点信息
     * 
     * @param id 服务网点ID
     * @return 结果
     */
    @Override
    public int deleteSysOutletsById(Long id)
    {
        return sysOutletsMapper.deleteSysOutletsById(id);
    }

    @Override
    public String getSN() {
        return sysOutletsMapper.getSN();
    }

    public SysOutlets getCoordinate(SysOutlets params) {
        if(!StringUtils.isBlank(params.getAddress()) && !StringUtils.isBlank(params.getRegion())){
            String query = params.getAddress().replace("-", "");
            String region = params.getRegion().replace("-", "");
            JSONObject object = CoordinateUtil.getCoordinate(region, query);
            if (null == object || object.isEmpty()) {
                throw new CustomException("region or address is error", 400);
            }
            params.setLongitude(object.getDouble("longitude"));
            params.setLatitude(object.getDouble("latitude"));
        }
        return params;
    }
}
